import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import ElementPlus from 'unplugin-element-plus/vite'

export default defineConfig(({ mode }) => {
  const isDev = mode === 'development'
  
  return {
    plugins: [
      vue(),
      
      // 自动导入Vue 3 APIs
      AutoImport({
        imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
        resolvers: [ElementPlusResolver()],
        dts: 'src/auto-imports.d.ts',
        vueTemplate: true,
        eslintrc: {
          enabled: false,
          filepath: './.eslintrc-auto-import.json',
          globalsPropValue: true
        }
      }),
      
      // 自动导入组件
      Components({
        resolvers: [ElementPlusResolver()],
        dts: 'src/components.d.ts',
        dirs: ['src/components', 'src/views']
      }),
      
      // Element Plus优化
      ElementPlus({
        useSource: true
      }),
      
      // Mock数据服务 (暂时禁用，使用内置Mock)
      // viteMockServe({
      //   mockPath: 'src/mock',
      //   enable: isDev,
      //   watchFiles: true,
      //   logger: true
      // })
    ],
    
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        '~': resolve(__dirname, 'src'),
        '@/mock': resolve(__dirname, 'src/mock'),
        '@/utils': resolve(__dirname, 'src/utils'),
        '@/config': resolve(__dirname, 'src/config'),
        '@/components': resolve(__dirname, 'src/components'),
        '@/views': resolve(__dirname, 'src/views'),
        '@/stores': resolve(__dirname, 'src/stores')
      }
    },
    
    // 移除CSS预处理配置，避免与Element Plus冲突
    // css: {
    //   preprocessorOptions: {
    //     scss: {
    //       additionalData: '@import "@/styles/variables.scss";'
    //     }
    //   }
    // },
    
    server: {
      host: '0.0.0.0',
      port: 3000,
      open: true,
      cors: true
      // 禁用API代理，使用Mock数据
      // proxy: {
      //   '/api': {
      //     target: 'http://localhost:3001',
      //     changeOrigin: true,
      //     rewrite: (path) => path.replace(/^\/api/, '')
      //   }
      // }
    },
    
    build: {
      target: 'es2015',
      outDir: 'dist',
      assetsDir: 'assets',
      sourcemap: isDev,
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: !isDev,
          drop_debugger: !isDev
        }
      },
      rollupOptions: {
        output: {
          chunkFileNames: 'js/[name]-[hash].js',
          entryFileNames: 'js/[name]-[hash].js',
          assetFileNames: '[ext]/[name]-[hash].[ext]',
          manualChunks: {
            vendor: ['vue', 'vue-router', 'pinia'],
            element: ['element-plus', '@element-plus/icons-vue'],
            echarts: ['echarts', 'vue-echarts'],
            utils: ['axios', 'dayjs', '@vueuse/core']
          }
        }
      }
    },
    
    optimizeDeps: {
      include: [
        'vue',
        'vue-router',
        'pinia',
        'element-plus',
        '@element-plus/icons-vue',
        'echarts',
        'vue-echarts',
        'dayjs',
        'axios',
        '@vueuse/core',
        'mockjs',
        '@faker-js/faker',
        'loglevel'
      ]
    },
    
    define: {
      __VUE_OPTIONS_API__: true,
      __VUE_PROD_DEVTOOLS__: false
    }
  }
})